package seis635.ui;

import seis635.design.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class ClockUI{
	private DateFormat timerFormat;
	private DateFormat timer12Format;
	private DateFormat timer24Format;
	private DateFormat dateFormat;
//	private Clock clk;
	private JPanel clkPanel;
	private JLabel clkTimeLabel;
	private JLabel clkDateLabel;
	private JPanel clkSouthPanel;
	private JLabel clkCurrentTimezoneLb;
	private JLabel clkDSTLabel;
	private JComboBox clkTimezoneComboBox;
	private JPanel clkComboBoxPn;
	private JCheckBox clk24HChk;
	java.util.Calendar cal;
	
	public JPanel getCLKPanel(){
		return clkPanel;
	}
	public ClockUI(){
//		this.clk = clk;
		cal = java.util.Calendar.getInstance();
		
		timerFormat = new SimpleDateFormat("h:mm:ss a");
		timer12Format = new SimpleDateFormat("h:mm:ss a ");
		timer24Format = new SimpleDateFormat("H:mm:ss ");
		dateFormat = new SimpleDateFormat("E yyyy, MMMM dd z ");
		
		
		clkPanel = new JPanel();
		clkPanel.setLayout(new BorderLayout(0, 0));
		long temp = System.currentTimeMillis();
		clkTimeLabel = new JLabel();
		clkTimeLabel.setHorizontalAlignment(SwingConstants.CENTER);
		clkTimeLabel.setFont(new Font("Lucida Grande", Font.BOLD | Font.ITALIC, 20));
		clkPanel.add(clkTimeLabel, BorderLayout.CENTER);
		
		JPanel clkNorthPanel = new JPanel();
		clkPanel.add(clkNorthPanel, BorderLayout.NORTH);
		clkNorthPanel.setLayout(new BorderLayout(0, 0));
		
		clkDateLabel = new JLabel(dateFormat.format(new java.util.Date(temp)));
		clkDateLabel.setVerticalAlignment(SwingConstants.BOTTOM);
		clkNorthPanel.add(clkDateLabel, BorderLayout.EAST);
		
		clkSouthPanel = new JPanel();
		clkPanel.add(clkSouthPanel, BorderLayout.SOUTH);
		clkSouthPanel.setLayout(new BorderLayout(0, 0));
		
		clk24HChk = new JCheckBox("24H ");
		clk24HChk.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				if (clk24HChk.isSelected()){
					timerFormat = timer24Format;
				}
				else{
					timerFormat = timer12Format;
				}
			}
		});
		
		clkSouthPanel.add(clk24HChk, BorderLayout.WEST);
		
		clkCurrentTimezoneLb = new JLabel("Timezone");
		clkSouthPanel.add(clkCurrentTimezoneLb, BorderLayout.CENTER);
		
		clkDSTLabel = new JLabel("DST");
		clkDSTLabel.setFont(new Font("Lucida Grande", Font.BOLD, 12));
		clkSouthPanel.add(clkDSTLabel, BorderLayout.EAST);
		
		clkComboBoxPn = new JPanel();
		clkSouthPanel.add(clkComboBoxPn, BorderLayout.SOUTH);
		clkComboBoxPn.setLayout(new BorderLayout(0, 0));
		
		clkTimezoneComboBox = new JComboBox();
		clkTimezoneComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
//				comboBoxItem = (String)clkTimezoneComboBox.getSelectedItem();
				timerFormat.setTimeZone(TimeZone.getTimeZone((String)clkTimezoneComboBox.getSelectedItem()));
				timer12Format.setTimeZone(TimeZone.getTimeZone((String)clkTimezoneComboBox.getSelectedItem()));
				timer24Format.setTimeZone(TimeZone.getTimeZone((String)clkTimezoneComboBox.getSelectedItem()));
				dateFormat.setTimeZone(TimeZone.getTimeZone((String)clkTimezoneComboBox.getSelectedItem()));
			}
		});
		
		clkTimezoneComboBox.setMaximumRowCount(16);
		clkComboBoxPn.add(clkTimezoneComboBox, BorderLayout.SOUTH);
		
		String [] list = TimeZone.getAvailableIDs();
		java.util.Arrays.sort(list);
		clkTimezoneComboBox.setModel(new DefaultComboBoxModel(list));
		clkTimezoneComboBox.setSelectedItem(cal.getTimeZone().getID());
		
		clkPanel.setVisible(true);
		
	}
	public void updateCLKTimeLabel (long time){
		clkTimeLabel.setText(timerFormat.format(new java.util.Date(time)));
	}
	public void updateCLKDateLabel (long time){
		clkDateLabel.setText(dateFormat.format(new java.util.Date(time)));
	}
	public void updateCLKCurrentTimezoneLb (String timezone){
		clkCurrentTimezoneLb.setText(timezone);
	}
	public void updateCLKDSTLabel (boolean status){
		String statusString;
		statusString = (status)? "DST: Yes " : "DTS: No ";
		clkDSTLabel.setText(statusString);
	}

	public String getComboBoxItem(){
		return (String) clkTimezoneComboBox.getSelectedItem();
	}
//	public void updateCLKLabel(long time){
//		//		System.out.println (timerFormat.format(new java.util.Date(time)));
//		clkLabel.setText(timerFormat.format(new java.util.Date(time)));
//		//		clkLabel.setText("test");
//	}
}